package com.hc.jdbc.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

//Jdbc工具类 1.0 版本
//实现连接
public class JdbcUtilsV1 {
    //连接池对象
    private static DataSource dataSource = null;

    static {
//初始化连接池对象
        Properties properties = new Properties();
        InputStream is =
                JdbcUtilsV1.class.getClassLoader().getResourceAsStream("druid.properties");
        try {
            properties.load(is);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        try {
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /*
    * 获取连接
    */
    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }

    /*
    * 回收连接
    * */
    public static void freeConnection(Connection connection) throws SQLException
    {
        connection.close();
    }

}
